from PySide6.QtCore import QFile, QDate
from PySide6.QtUiTools import QUiLoader

from src.main.server.controller.server_info_controller import ServerInfoController
from src.main.server.utils.format_datetime import format_datetime


class AddServerProjectWindow:
    def __init__(self, main_window, title, data = None):
        self.original_place = None
        self.original_name = None
        self.title = title
        self.data = None
        self.update = None
        if data:
            self.data = data
            self.update = True
        self._load_ui()
        self.main_window = main_window

    def _load_ui(self):
        path = "../../../qt_ui/add_server_project.ui"
        file = QFile(path)
        try:
            loader = QUiLoader()
            self.window = loader.load(file)
            self.window.label_7.setText(self.title)
            self.window.pushButton.clicked.connect(self.submit)
            self.window.pushButton_2.clicked.connect(self.close_window)

        finally:
            file.close()

        if self.window:
            self.window.show()  # 显示独立窗口
        else:
            print("加载 UI 失败，请检查文件路径或内容")

        if self.data:
            """有数据则回显数据"""
            self.get_info()

    def close_window(self):
        """关闭当前窗口"""
        self.window.close()

    def submit(self):
        server_name = self.window.lineEdit.text()
        place = self.window.lineEdit_2.text()
        info = self.window.plainTextEdit.toPlainText()
        time_start = self.window.dateEdit.date().toString("yyyy-MM-dd")
        time_end = self.window.dateEdit_2.date().toString("yyyy-MM-dd")
        volunteers_needed = self.window.spinBox.value()  # 获取 QSpinBox 的值

        print("| 项目名称:", server_name)
        print("| 地点:", place)
        print("| 信息:", info)
        print("| 开始日期:", time_start)
        print("| 结束日期:", time_end)
        print("| 所需志愿者人数:", volunteers_needed)

        if self.update:
            print("目前是修改操作")
            ServerInfoController().update((server_name, place, info, time_start, time_end, volunteers_needed, self.original_name, self.original_place))
        else:
            print("增加")
            ServerInfoController().add((server_name, place, info, time_start, time_end, volunteers_needed))
        self.main_window.show_server_project_data()
        self.close_window()

    def get_info(self):
        """查询回显"""

        print(self.data)
        self.window.lineEdit.setText(self.data[0][0])
        self.original_name = self.data[0][0]
        self.window.lineEdit_2.setText(self.data[0][1])
        self.original_place = self.data[0][1]
        self.window.plainTextEdit.setPlainText(self.data[0][2])

        # 设置 QDateEdit 的日期值
        start_date_str = format_datetime(self.data[0][3]).split()[0]
        end_date_str = format_datetime(self.data[0][4]).split()[0]

        start_date = QDate.fromString(start_date_str, "yyyy-MM-dd")
        end_date = QDate.fromString(end_date_str, "yyyy-MM-dd")

        if start_date.isValid():
            self.window.dateEdit.setDate(start_date)
        if end_date.isValid():
            self.window.dateEdit_2.setDate(end_date)

        self.window.spinBox.setValue(int(self.data[0][5]))


# 使用时
def add_server_project_window(self, title, data = None):
    if data:
        self.add_window = AddServerProjectWindow(self, title, data)
    else:
        self.add_window = AddServerProjectWindow(self, title)

